<?php

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('todo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

function dbq($sql)
{
	$q = mysql_query($sql) or die("<pre>".mysql_error()."<br/>\n".$sql."</pre>");
	return $q;
}
function dbq1($sql)
{
	$q = mysql_query($sql) or die(mysql_error());
	while($r = mysql_fetch_array($q))
	{
		return $r[0];
	}
}

function debug($str, $desc=false)
{
	echo "<pre style='border: 1px solid silver;'>";
	echo $desc."\n";
	print_r($str);
	echo "</pre>";
}

function get_recu($pid=0, $depth=0, $zoom_id=0)
{
	$res = dbq("select * from todos where date_finished IS NULL AND pid='".$pid."' order by prio, severity, txt");
	while($r = mysql_fetch_assoc($res))
	{
		if($depth == 0)
		{
			if($zoom_id == 0)
			{
				$color = "blue";
			}
			else
			{
				$color = "gray";			
			}
			echo "<tr>";
			echo "<td colspan='9' style='height:2px; background-color:".$color.";'>";
			echo "</td>";
			echo "</tr>";		
		}
		
		echo "<tr onmouseover='this.className=\"changeColorOnMouseOverTR\";' onmouseout='this.className=\"changeColorOnMouseOutTR\";'>";
		echo "<td style='text-align:center;'>";
		echo "<a href='?zoom_id=".$r['id']."'>";
		echo "<img src='pics/system-search.png' />";
		echo "</a>";
		echo "</td>";

		echo "<td style='text-align:center;'>";
		echo "<a href='edittask.php?id=".$r['id']."&zoom_id=".$zoom_id."'/>";
#		echo "<a href='?zoom_id=".$r['id']."'>";
		echo "<img src='pics/edit-find-replace.png' />";
		echo "</a>";
		echo "</td>";

		
		
		echo "<td style='padding-left:".($depth*10)."px;'>";
		
			echo "<table style='border-collapse:collapse;'>";
			echo "<tr>";
			echo "<td style='padding-left: 10px; padding-right: 10px;'>";
			
#			echo "<a href='edittask.php?id=".$r['id']."&zoom_id=".$zoom_id."'/>";
			
			

				$task = $r['txt'];

				// Check for minitasks
				$minitask_start_signal = "{{";
				$minitask_stop_signal = "}}";
			
				$worked_task = array();

				if(strpos($task, $minitask_start_signal))
				{	// Task includes minitask
					$breakup = explode($minitask_start_signal, $task);
					
					foreach($breakup AS $line)
					{
						$builder = "";
						
						if(strpos($line, $minitask_stop_signal))
						{
							$stop = strpos($line, $minitask_stop_signal);
							$minitask = substr($line, 0, $stop);
							
							$taskcode = "pid:".$r['pid']."_id:".$r['id']."_".urlencode($minitask);
#							debug($taskcode);

							$minitask_finished = false;
							
							if(dbq1("select count(*) from minitodos where taskcode='".$taskcode."'"))
							{	// minitask exists
								// get status
								
								if(dbq1("select date_finished from minitodos where taskcode='".$taskcode."' AND date_finished IS NOT NULL"))
								{	// taskcode is finished
									$minitask_finished = true;
								}								
							}
							else
							{	// minitask DOESNT exist
								// create minitask
								
								dbq("insert into minitodos (taskcode) values ('".$taskcode."')");
							}
							
							if($minitask_finished)
							{
								$bg = "#8f8";
							}
							else
							{
								$bg = "#fbb";
							}

							# &radic; er kvadratrod tegn
							
							$taskcode_id = dbq1("SELECT id FROM minitodos where taskcode='".$taskcode."'");
							
#							$line = "<a style='background-color:".$bg.";' href='pointer.php'>".$minitask_start_signal.$line;
							$line = "<a style='background-color:".$bg.";' href='switchminitask.php?id=".$taskcode_id."&state=".(($minitask_finished)?1:0)."&zoom_id=".$zoom_id."'>".$line;

#							$line = str_replace($minitask_stop_signal, $minitask_stop_signal."</a>", $line);
							$line = str_replace($minitask_stop_signal, "</a>", $line);
							$builder = $line;
						}
						else
						{
							$builder = $line;
						}
						$worked_task[] = $builder;
					}
				}
				else
				{
					$worked_task[0] = $task;
				}

				$parsed = "";
				foreach($worked_task AS $line)
				{
					$parsed .= $line;
				}
				
				echo $parsed;
				echo "<p>";

				

			echo "</td>";
			echo "</tr>";
			echo "</table>";
		
		echo "</td>";

		echo "<td style='text-align:center;'>";
		echo "<a href='addtask.php?pid=".$r['id']."&zoom_id=".$zoom_id."'>";
#		echo "<img src='pics/list-add.png' />";
		echo "<img src='pics/window-new.png' />";
		echo "</a>";
		echo "</td>";

		echo "<td align='middle'>";		
			echo "<table>";
			echo "<tr>";
			echo "<td align='left' width='10'>";
			echo "<a href='change.php?id=".$r['id']."&what=prio&direction=down&zoom_id=".$zoom_id."'>";
			echo "&darr;";
			echo "</a>";
			echo "</td>";
			echo "<td align='middle' width='15'>";
			echo $r['prio'];
			echo "</td>";
			echo "<td align='right' width='10'>";
			echo "<a href='change.php?id=".$r['id']."&what=prio&direction=up&zoom_id=".$zoom_id."'>";
			echo "&uarr;";
			echo "</a>";
			echo "</td>";
			echo "</tr>";
			echo "</table>";
		echo "</td>";

		echo "<td align='middle'>";
			echo "<table>";
			echo "<tr>";
			echo "<td align='left' width='10'>";
			echo "<a href='change.php?id=".$r['id']."&what=severity&direction=down&zoom_id=".$zoom_id."'>";
			echo "&darr;";
			echo "</a>";
			echo "</td>";
			echo "<td align='middle' width='15'>";
			echo $r['severity'];
			echo "</td>";
			echo "<td align='right' width='10'>";
			echo "<a href='change.php?id=".$r['id']."&what=severity&direction=up&zoom_id=".$zoom_id."'>";
			echo "&uarr;";
			echo "</a>";
			echo "</td>";
			echo "</tr>";
			echo "</table>";
		echo "</td>";

		echo "<td align='middle'>";		
			echo "<table>";
			echo "<tr>";
			echo "<td align='left' width='10'>";
			echo "<a href='change.php?id=".$r['id']."&what=hours&direction=down&zoom_id=".$zoom_id."'>";
			echo "&darr;";
			echo "</a>";
			echo "</td>";
			echo "<td align='middle' width='15'>";
			echo $r['hours'];
			echo "</td>";
			echo "<td align='right' width='10'>";
			echo "<a href='change.php?id=".$r['id']."&what=hours&direction=up&zoom_id=".$zoom_id."'>";
			echo "&uarr;";
			echo "</a>";
			echo "</td>";
			echo "</tr>";
			echo "</table>";
		echo "</td>";

		echo "<td align='middle'>";		
#		$numsubs=0;
		$result=$r['hours'];
		$sub_all_hours = get_sub_all_hours($r['id'], $result);
		
		if($r['hours'] == $result)
		{
			echo "&nbsp;";
		}
		else
		{
			echo $result;
		}
		
		echo "</td>";

/*		echo "<td>";
		# days old
		echo time();
		echo "<br />";
		echo strtotime($r['date_added']);
		echo "</td>";
*/
		echo "<td style='text-align:center; font-size:10px;'>";
		echo "<a onclick='return confirm(\"Really mark as Done?\");' href='finishtask.php?id=".$r['id']."&zoom_id=".$zoom_id."'>Done!</a>";
/*		
			echo "<table>";
			echo "<form action='finishtask.php?id=".$r['id']."&zoom_id=".$zoom_id."' method='POST'>";
			echo "<tr>";
			echo "<td>";

			echo "<input type='text' name='actual_task_time' value='".$r['hours']."' size='1' />";
			echo "<input type='submit' name='submitted' value='&gt;' />";
			
			echo "</td>";
			echo "</tr>";
			echo "</form>";
			echo "</table>";
*/		
		echo "</td>";
		echo "</tr>";
		
		get_recu($r['id'], $depth+1, $zoom_id);
	}
}

function get_sub_all_hours($pid, &$result)
{
	$res = dbq("select id,hours from todos where date_finished IS NULL AND pid='".$pid."'");
	while($r = mysql_fetch_assoc($res))
	{
		$result += $r['hours'];
#		echo $r['hours'];
#		echo "+";
		get_sub_all_hours($r['id'], $result);
	}
}


function getBackPath($id)
{
	$res = dbq("select id,pid,txt from todos where id = '".$id."'");
	while($r = mysql_fetch_assoc($res))
	{
		getBackPath($r['pid']);
		
		echo "&nbsp;";
		echo "<img src='pics/media-playback-start.png' style='vertical-align:middle;' />";
		echo "&nbsp;";
		echo "<a href='index.php?zoom_id=".$r['id']."'>";
		echo $r['txt'];
		echo "</a>";
	}
}

function get_recu_select($pid=0, $depth=0)
{
	$chosen_pid = dbq1("select pid from todos where id='".$_GET['id']."'");
	
	$res = dbq("select * from todos where date_finished IS NULL AND pid='".$pid."'");
	while($r = mysql_fetch_assoc($res))
	{
		if($r['id'] != $_GET['id'])
		{
			echo "\n";
			echo "<option ";
			if($chosen_pid == $r['id'])
			{
				echo "selected='selected' ";
			}
			echo "value='".$r['id']."'>";
			for($i=0; $i<$depth; $i++)	{	echo "--";	}
#			echo "id:".$r['id'].",";
#			echo "pid:".$pid.",";
			echo $r['txt'];
			echo "</option>\n";

			get_recu_select($r['id'], $depth+1);
		}
	}
}



?>